import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';

class BaseWidget extends StatelessWidget{

  final titles = ["Widget管理自身状态","父Widget管理子Widget的状态","混合状态管理"];

  Iterable<WordPair> englishWords = generateWordPairs().take(10);
  List<bool> switchs = [false,false,false,false,false,false,false,false,false,false];

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("基础组件"),
      ),
      body: ListView.separated(
          itemCount: 3,
          itemBuilder: (context,i){
            return ListTile(title: Text(this.titles[i]),onTap: ()=> _didTouchCellForRow(context,i),);
          },
          separatorBuilder: (context,i){
            return Divider();
          },
      ),
    );
  }

  void _didTouchCellForRow(context,i){

    if (i == 0){
      Navigator.pushNamed(context, "widget_tapboxA");
    }

    if (i == 1){
      Navigator.pushNamed(context, "widget_tapboxB",arguments: this.titles[i]);
    }

    if (i == 2){
      Navigator.pushNamed(context, "TapBoxCListVC",arguments: {"englishWords":this.englishWords,"switchs":this.switchs});
    }
  }
}

class TapBoxCListVC extends StatelessWidget{

  TapBoxCListVC({Key key}):super(key: key);
  Iterable<WordPair> englishWords;
  List<bool> switchs;

  @override
  Widget build(BuildContext context) {


    final args = ModalRoute.of(context).settings.arguments as Map;
    this.englishWords = args["englishWords"];
    this.switchs = args["switchs"];
    debugPrint("====== params:$switchs");
    return Scaffold(
      appBar: AppBar(title: Text("状态混合管理"),),
      body: Container(
        color: Colors.white,
        child: ListView.separated(
          padding: EdgeInsets.all(16.0),
          itemCount: englishWords.length,
          itemBuilder: (context,i){
            return TapBoxC(title: this.englishWords.toList()[i].asPascalCase,valueChanged: _cellDidTouchForRow,index: i,isActive: this.switchs[i],);
          },
          separatorBuilder: (context,i){
            return Divider();
          },
        ),
      )
    );
  }

  void _cellDidTouchForRow(TapBoxCTouchCallBackModel model){
    debugPrint("======= idx: "+model.index.toString()+" isActive:"+ model.isActive.toString());
    this.switchs[model.index] = model.isActive;
    debugPrint("====== params:$switchs");
  }
}

class TapBoxCTouchCallBackModel extends Object{
  int index;
  bool isActive;
}

class TapBoxC extends StatefulWidget {

  TapBoxC({Key key,@required this.title,this.isActive:false,@required this.index,this.valueChanged}) : super(key: key);

  final int index;
  final String title;
  bool isActive;
  final ValueChanged<TapBoxCTouchCallBackModel> valueChanged;

  @override
  TapBoxCState createState() => TapBoxCState();
}

class TapBoxCState extends State<TapBoxC>{

  bool isHighlight = false;

  void _tapUpGesture(TapUpDetails details){
    setState(() {
      isHighlight = false;
    });
  }

  void _tapDownGesture(TapDownDetails details){
    setState(() {
      isHighlight = true;
    });
  }

  void _tapCancelGesture(){
    setState(() {
      isHighlight = false;
    });
  }

  /*点击事件*/
  void _tapTouchGesture(){
    setState(() {
      widget.isActive = !widget.isActive;
    });
    TapBoxCTouchCallBackModel m = TapBoxCTouchCallBackModel();
    m.isActive = widget.isActive;
    m.index = widget.index;
    widget.valueChanged(m);
  }

  /*获取开关状态*/
  String _getSwitchStatusStr(){
    return widget.isActive ? "已开启" : "已关闭";
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return GestureDetector(
      onTap: _tapTouchGesture,
      onTapCancel: _tapCancelGesture,
      onTapDown: _tapDownGesture,
      onTapUp: _tapUpGesture,
      child: Container(
        padding: EdgeInsets.zero,
        margin: EdgeInsets.zero,
        width: MediaQuery.of(context).size.width,
        height: 50,
        child: Row(
          children: [
            Text(widget.title,textAlign: TextAlign.left,style: TextStyle(color: widget.isActive ? Colors.red : Colors.black),),
            Spacer(),
            Text(_getSwitchStatusStr(),textAlign: TextAlign.left,style: TextStyle(color: widget.isActive ? Colors.red : Colors.black))
          ],
        ),
        // color: isHighlight ? Colors.lightGreen[100] : Colors.white,
        decoration: BoxDecoration(
          color: isHighlight ? Colors.lightGreen[100] : Colors.white,
        ),
      ),
    );
  }
}

class TapBoxB extends StatefulWidget {
  TapBoxB({Key key,@required this.title}) : super(key: key);

  final String title;

  @override
  TapBoxBState createState() => TapBoxBState(onChanged: _onTouchChanged);

  /*选中回调*/
  void _onTouchChanged(isActive){
     final txt = isActive ? "Active" : "Inactive";
     debugPrint("====== $txt");
  }
}

class TapBoxBState extends State<TapBoxB>{

  TapBoxBState({Key key,this.isActive:false,@required this.onChanged}) : super();

  bool isActive;
  final ValueChanged<bool> onChanged;

  void _onTouchChagned(){
    setState(() {
      this.isActive = !this.isActive;
    });
    this.onChanged(isActive);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(title: Text(widget.title),),
      body: GestureDetector(
        onTap: _onTouchChagned,
        child: Center(
          child: Container(
            child: Center(
              child: Text(isActive ? "Active" : "Inactive",textAlign: TextAlign.center,style: TextStyle(color: Colors.white,fontSize: 30),),
            ),
            width: 200,
            height: 200,
            decoration: BoxDecoration(
              color: isActive ? Colors.lightGreen[700] : Colors.grey[600]
            ),
          ),
        ),
      )
    );
  }
}


class TapBoxA extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return TapBoxAState();
  }
}

class TapBoxAState extends State<StatefulWidget>{

  bool _isActive = false;

  void _boxTouch(){
    setState(() {
      this._isActive = !this._isActive;
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("自身状态管理"),
      ),
      body: GestureDetector(
        onTap: _boxTouch,
        child: Center(
          child: Container(
              child: Center(
                child: Text(
                  _isActive ? "Active" : "Inactive",
                  style: TextStyle(fontSize: 20.0,color: Colors.black),
                  textAlign: TextAlign.center,
                ),
              ),
              width: 200,
              height: 200,
              decoration: BoxDecoration(
                  color: _isActive ? Colors.lightGreen[700] : Colors.grey[600]
              )
          ),
        ),
        )
    );
  }
}